//
//  CertifyConditionView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CertifyConditionView: BaseView {
    var viewModel:CertifyConditionViewModel?
    lazy var nameLable:BaseLabel = {
       let label = BaseLabel(style: .oneStyle)
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    
    lazy var headPhoto:UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 25
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    lazy var idCardLable:BaseLabel = {
        let label = BaseLabel(style: .oneStyle)
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    override func setUpUI() {
        super.setUpUI()
        viewModel = self.dataSource as? CertifyConditionViewModel
        
        viewModel?.addObserver(self, forKeyPath: "refreshBool", options: [.new,.old], context: nil)
        
        if (viewModel?.isSuccess)! {
            let layerView = UIView()
            self.addSubview(layerView)
            layerView.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(80)
                make.left.equalToSuperview().offset(15)
                make.right.equalToSuperview().offset(-15)
                make.height.equalTo(150)
            }
            layerView.layer.cornerRadius = 5
            layerView.layer.borderColor = UIColor.black.withAlphaComponent(0.2).cgColor
            layerView.layer.borderWidth = 0.5
            layerView.layer.shadowColor = UIColor.black.cgColor
            layerView.layer.shadowOffset = CGSize(width: 3, height: 3)
            layerView.layer.shadowOpacity = 1;
            
            
            let inforLabel = UILabel()
            inforLabel.text = kText("已实名认证")
            inforLabel.textColor = kThemeColor
            inforLabel.font = UIFont.systemFont(ofSize: 15)
            self.addSubview(inforLabel)
            inforLabel.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(125)
                make.centerX.equalToSuperview()
            }
            
            self.addSubview(nameLable)
            nameLable.snp.makeConstraints { (make) in
                make.top.equalTo(inforLabel.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
            }
            
            self.addSubview(headPhoto)
            headPhoto.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.bottom.equalTo(layerView.snp.top).offset(25)
                make.height.width.equalTo(50)
            }
            
            self.addSubview(idCardLable)
            idCardLable.snp.makeConstraints { (make) in
                make.top.equalTo(nameLable.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
            }
            
            if let urlstr = AppInfo.default.portrait {
                headPhoto.sd_setImage(with: URL(string: urlstr), placeholderImage: #imageLiteral(resourceName: "login_icon"))
            }else {
                headPhoto.image = #imageLiteral(resourceName: "login_icon")
            }
            
        }else {
            let imageView = UIImageView(image: UIImage(named: "certysh"))
            self.addSubview(imageView)
            imageView.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(80)
                make.centerX.equalToSuperview()
            }
            
            let textLabel = BaseLabel(style: .oneStyle)
            textLabel.text = kText("认证信息已提交，正在审核")
            textLabel.font = UIFont.systemFont(ofSize: 18)
            textLabel.textAlignment = NSTextAlignment.center
            textLabel.numberOfLines = 0
            self.addSubview(textLabel)
            textLabel.snp.makeConstraints { (make) in
                make.top.equalTo(imageView.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
                make.left.equalToSuperview().offset(15)
                make.right.equalToSuperview().offset(-15)
            }
            
            let inforLabel = BaseLabel(style: .twiceStyle)
            inforLabel.text = kText("审核需要1～2个工作日的时间，请留意您的审核状态")
            inforLabel.font = UIFont.systemFont(ofSize: 12)
            inforLabel.textAlignment = NSTextAlignment.center
            inforLabel.numberOfLines = 0
            self.addSubview(inforLabel)
            inforLabel.snp.makeConstraints { (make) in
                make.top.equalTo(textLabel.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
                make.left.equalToSuperview().offset(15)
                make.right.equalToSuperview().offset(-15)
            }
            
        }
        
        
    }
    
    /// KVO监听属性
    ///
    /// - Parameters:
    ///   - keyPath: 被监听的属性名
    ///   - object:
    ///   - change:
    ///   - context:
    @objc override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "refreshBool" {
            self.nameLable.text = String(format: "*%@",(viewModel?.model.userName.count)! > 1 ?
                ((viewModel?.model.userName as NSString?)?.substring(from: (viewModel?.model.userName.count)! - 1))! : "")
            self.idCardLable.text = viewModel?.model.credentialsCode.count != 0 ?  viewModel?.model.credentialsCode : ""
        }
        
    }
    
    
    
    
    deinit {
        self.viewModel?.removeObserver(self, forKeyPath: "refreshBool", context: nil)
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
